#include <iostream>
#include <mpi.h>

int main(int argc, char** argv) {
    int nrank, nproc;
    int iermpi;

    // 初始化 MPI 环境
    iermpi = MPI_Init(&argc, &argv);
    if (iermpi != MPI_SUCCESS) {
        std::cerr << "MPI_Init failed with error code " << iermpi << std::endl;
        return 1;
    }

    // 获取当前进程的编号和总进程数
    iermpi = MPI_Comm_rank(MPI_COMM_WORLD, &nrank);
    if (iermpi != MPI_SUCCESS) {
        std::cerr << "MPI_Comm_rank failed with error code " << iermpi << std::endl;
        MPI_Finalize();
        return 1;
    }

    iermpi = MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    if (iermpi != MPI_SUCCESS) {
        std::cerr << "MPI_Comm_size failed with error code " << iermpi << std::endl;
        MPI_Finalize();
        return 1;
    }

    // 打印进程信息
    std::cout << "进程编号: " << nrank << ", 总进程数: " << nproc << std::endl;

    // 同步所有进程
    iermpi = MPI_Barrier(MPI_COMM_WORLD);
    if (iermpi != MPI_SUCCESS) {
        std::cerr << "MPI_Barrier failed with error code " << iermpi << std::endl;
    }

    // 清理 MPI 环境
    iermpi = MPI_Finalize();
    if (iermpi != MPI_SUCCESS) {
        std::cerr << "MPI_Finalize failed with error code " << iermpi << std::endl;
        return 1;
    }

    return 0;
}
